package ru.rs.interfaces.impl;

import java.io.IOException;
import java.io.InputStream;

import ru.rs.interfaces.Graphics;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;

public class GraphicsImpl implements Graphics {

	private Bitmap frame;

	private Canvas canvas;

	private AssetManager manager;

	public GraphicsImpl(AssetManager assets, Bitmap frame) {

		this.frame = frame;
		manager = assets;
		canvas = new Canvas(this.frame);
	}

	public Bitmap newBitmap(String fileName) {

		InputStream in = null;
		Bitmap bm = null;
		try {
			in = manager.open(fileName);
			bm = BitmapFactory.decodeStream(in);
			if (bm == null) {
				throw new RuntimeException("No file " + fileName);
			}
		} catch (IOException e) {

		} finally {
			if (in != null) {
				try {
					in.close();

				} catch (IOException e) {
				}
			}
		}

		return bm;
	}

	public void drawBitmap(Bitmap bm, int x, int y) {

		canvas.drawBitmap(bm, x, y, null);
	}

	public void clear(int color) {

		canvas.drawColor(color);
	}

	public int getWidth() {

		// TODO Auto-generated method stub
		return frame.getWidth();
	}

	public int getHeight() {

		// TODO Auto-generated method stub
		return frame.getHeight();
	}

	public void drawText(String text, int x, int y, int color) {

		Paint paint = new Paint();
		paint.setColor(color);
		paint.setTextSize(16);
		canvas.drawText(text, x, y, paint);
	}
}
